﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2010-12-09 21:03:24Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

using Outcoder.Data;

#if WINDOWS_PHONE
using Microsoft.Phone.Tasks;
#endif

namespace Outcoder.LauncherModel.Launchers
{
	/* TODO: [DV] Comment. */
	public class MockPhotoChooserLauncher : IPhotoChooserLauncher
	{
		MockPhotoResult photoResult;

		public MockPhotoResult PhotoResult
		{
			get
			{
				return photoResult;
			}
			set
			{
				photoResult = value;
			}
		}

#if WINDOWS_UWP || NETFX_CORE
		public async Task<PhotoResultBase> ShowAsync()
		{
			Debug.WriteLine(string.Format("MockPhotoChooserTask shown PixelHeight {0}, PixelWidth {1}, ShowCamera {2}",
				PixelHeight, PixelWidth, ShowCamera));

			if (photoResult != null)
			{
				OnCompleted(photoResult);
				return new MockPhotoResult();
			}

			string assemblyShortName = typeof(MockPhotoChooserLauncher).GetTypeInfo().Assembly.FullName;
			assemblyShortName = assemblyShortName.Substring(0, assemblyShortName.IndexOf(','));
			var resourceUri = new Uri("ms-appx:///" + assemblyShortName + "/LauncherModel/Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png", UriKind.Relative);

			Stream photoStream = await ApplicationResourceUtility.GetApplicationResourceStreamAsync(resourceUri);

			photoResult = new MockPhotoResult
			{
				OriginalFileName = "Calcium_PhotoChooserTask.png",
				/* TODO: Resize image according to PixelHeight and PixelWidth. */
				ChosenPhoto = photoStream,
			};
			photoResult.SetTaskResult(TaskResult.OK);
			OnCompleted(photoResult);

			return photoResult;
		}
#endif

		public async void Show()
		{
			Debug.WriteLine(string.Format("MockPhotoChooserTask shown PixelHeight {0}, PixelWidth {1}, ShowCamera {2}",
				PixelHeight, PixelWidth, ShowCamera));

			if (photoResult != null)
			{
				OnCompleted(photoResult);
				return;
			}

#if WINDOWS_UWP || NETFX_CORE
			string assemblyShortName = typeof(MockPhotoChooserLauncher).GetTypeInfo().Assembly.FullName;
			assemblyShortName = assemblyShortName.Substring(0, assemblyShortName.IndexOf(','));
			var resourceUri = new Uri("ms-appx:///" + assemblyShortName + "/LauncherModel/Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png", UriKind.Relative);
#elif __ANDROID__
			var context = Android.App.Application.Context;
			var photoStream = context.Assets.Open("Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png");
#else
			string assemblyShortName = Assembly.GetExecutingAssembly().FullName;
			assemblyShortName = assemblyShortName.Substring(0, assemblyShortName.IndexOf(','));
			var resourceUri = new Uri("/" + assemblyShortName + ";component/LauncherModel/Launchers/PhotoChooserLauncher/PhotoChooserLauncher.png", UriKind.Relative);
#endif

#if !__ANDROID__
			Stream photoStream = await ApplicationResourceUtility.GetApplicationResourceStreamAsync(resourceUri);
#endif
			photoResult = new MockPhotoResult
			{
				OriginalFileName = "Calcium_PhotoChooserTask.png",
				/* TODO: Resize image according to PixelHeight and PixelWidth. */
				ChosenPhoto = photoStream,
			};
			photoResult.SetTaskResult(TaskResult.OK);
			OnCompleted(photoResult);
		}

		public event EventHandler<PhotoResultBase> Completed;

		protected virtual void OnCompleted(PhotoResultBase e)
		{
			var tempHandler = Completed;
			if (tempHandler != null)
			{
				tempHandler(this, e);
			}
		}

		public int PixelHeight { get; set; }
		public int PixelWidth { get; set; }
		public bool ShowCamera { get; set; }

	}
}
